import time
import SimpleHTTPServer
#import http.server
import SocketServer

BYTES_PER_SECOND=160*1024

class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        """Serve a GET request."""
        f = self.send_head()
        if f:
            self.copyfileobj(f, self.wfile)
            f.close()
    def copyfileobj(self,fsrc, fdst, length=16*1024):
        """copy data from file-like object fsrc to file-like object fdst"""
        count = 0
        t1 = time.time()
        while 1:
            buf = fsrc.read(length)
            if not buf:
                break
            fdst.write(buf)
            count += len(buf)
            if count >= BYTES_PER_SECOND:
                count = 0
                delay = 1.0 - (time.time() - t1)
                if delay > 0.0:
                    time.sleep(delay)
                t1 = time.time()                
PORT = 8080
Handler = MyHTTPRequestHandler
httpd = SocketServer.TCPServer(("127.0.0.1", PORT), Handler)
print ("serving at port", PORT)
httpd.serve_forever()
